<?php


namespace app\admin\controller;


use think\facade\View;

class Banner extends AdminBase {
    protected $BannerCategoryId = 1;

    public function index() {
        //从文章表中获取banner
        try {
            $bannerList = (new \app\admin\business\Banner())->getBannerList($this->BannerCategoryId);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
//        return show(1, 'testBanner', $bannerList);
        return View::fetch('', [
            'list' => $bannerList,
            'categoryId' => $this->BannerCategoryId
        ]);
    }

    public function add() {
        if ($this->request->isGet()) {
            return View::fetch();
        } elseif ($this->request->isPost()) {
            $category_id = $this->BannerCategoryId;
            $title = $this->request->param('title', '');
            $item_number = $this->request->param('item_number', '');
            $image_url = $this->request->param('image_url', '');
            $author = $this->request->param('author', '');
            $content = $this->request->param('content', '');
            $data = [
                'category_id' => $category_id,
                'title' => $title,
                'item_number' => $item_number,
                'is_banner' => 1,
                'image_url' => $image_url,
                'author' => $author,
                'content' => $content,
                'update_time' => time(),
                'create_time' => time(),
            ];
            $validate = new \app\admin\validate\Banner();
            if (!$validate->check($data)) {
                return show(config("status.error"), $validate->getError());
            }
            try {
                $articleObj = new \app\admin\business\Article();
                $res = $articleObj->saveArticle($data);

            } catch (\Exception $e) {
                return show(config("status.error"), $e->getMessage());
            }
            if (!$res) {
                return show(config("status.error"), '上传banner失败');
            }
            return show(config("status.success"), '上传banner成功');
        }
        return show(config('status.error'), '请求方式不正确');
    }

    public function showImage() {
        $id = $this->hasGetId();
        try {
            $imageUrl = (new \app\admin\model\Article())->getBannerImageById($id);
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        return View::fetch('', ['banner' => $imageUrl]);
    }

    public function changeImage() {
        $id = $this->hasPostId();
        $imageUrl = $this->request->param('image_url', '');
        try {
            $article = (new \app\admin\model\Article())->find($id);
            $article->image_url = $imageUrl;
            $article->update_time = time();
            $res = $article->save();
        } catch (\Exception $e) {
            return show(config('status.error'), $e->getMessage());
        }
        if (!$res) {
            return show(config('status.error'), '更换图片失败');
        }
        return show(config('status.success'), '更换图片成功');
    }
}